/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.miscellaneous.io;

import java.io.InputStream;
import java.io.IOException;

import java.util.Iterator;
import java.util.List;

import phoside.PhosphoProteins;
import phoside.Protein;

import phoside.classify.result.PhosidePredictionResult;
import phoside.classify.result.PhosidePredictionResultImpl;

import phoside.io.ProteinsReader;
import phoside.io.xml.PhosideXmlProteinsReader;
import phoside.io.xml.PhosphoProteinFieldValueFormatter;

import phoside.util.IOUtil;
import phoside.util.ClientHttpRequest;

/**
 *
 * @author gjj
 */
public class PhosphoReaderPkaPs implements ProteinsReader {
    private PhosidePredictionResult data;
    private String dirReport;

    public PhosphoReaderPkaPs(PhosidePredictionResult data,
            String dirReport) {
        this.data = data;
        this.dirReport = dirReport;
    }

    public PhosphoProteins read() throws IOException {
        List<String> lines = IOUtil.readStringListAscii(dirReport);
        String acc = null;
        for (String line:lines) {
            String[] strs = line.split("\t");
            if (strs[0].length()>0) {
                acc = strs[0];
            }

            int site = Integer.parseInt(strs[1])-1;
            data.addSite(acc, site);

            double score = Double.parseDouble(strs[2]);
            data.setPrediction(acc, site, score);
        }        

        return data;
    }

    public void saveResult(String dirSave) throws IOException {
        //FileWriter fout = new FileWriter(dirSave);
        //BufferedWriter out = new BufferedWriter(fout);

        Iterator<Protein> it = data.proteinIterator();
        while (it.hasNext()) {
            Protein protein = it.next();
            String acc = protein.getAccession();

            //if (!acc.startsWith("EN")) continue;

            //out.write(">"+acc);
            //out.newLine();

            try {
                InputStream is = ClientHttpRequest.post(
                                  new java.net.URL("http://mendel.imp.ac.at/sat/pkaPS/cgi-bin/webjob_pkaPS.cgi"),
                                  new Object[] {
                                                "sequence", ">"+acc+"\n"+protein.getSequence(),
                                                "formatting", "2",
                                                "hits ", '\03'
                                               });

                java.util.List<String> lines = IOUtil.readStringListAscii(new java.io.InputStreamReader(is));

                IOUtil.writeCollectionAscii(lines, dirSave+java.io.File.separator+acc+".html");

            } catch (Exception e) {
                System.err.println("\t"+acc);
                e.printStackTrace();
                continue;
            }

            System.out.println(acc);
            break;

        }

//        out.close();
//        fout.close();
    }

    public static void main(String[] args) {
        String testXml = "exp\\test1\\kinase-specific\\PKA\\Human-UniProt-SwissProt-V15.8-20090922-PhosphoELM-V8.2-April2009-nr50-PKA-test-without-sites";
        String rawres = "exp\\test1\\kinase-specific\\PKA\\PKAPS.raw.txt";

        PhosidePredictionResult data = new PhosidePredictionResultImpl(null);
        PhosideXmlProteinsReader readerxml = new PhosideXmlProteinsReader(testXml+".xml", data,
                new PhosphoProteinFieldValueFormatter());

        PhosphoReaderPkaPs reader = new PhosphoReaderPkaPs(data,rawres);

        phoside.io.xml.PhosideXmlProteinsWriter writer
                = new phoside.io.xml.PhosideXmlProteinsWriter(testXml+".pkaps.xml",
                  new phoside.io.xml.PhosphoProteinFieldValueFormatter());

        try {
            readerxml.read();
            for (String acc : data.getProteinsAccessions()) {
                data.clearSites(acc);
                data.getProtein(acc).removeInfo(phoside.util.StaticFinalValues.DISORDER);
            }

            //reader.saveResult(rawres);

            reader.read();

            writer.write(data);
        } catch(IOException e) {
            e.printStackTrace();
        }

    }
}
